#include "../include/head.h"

/**
 * @note 函数调用方法：
 * 1) 记录服务端启动日志：
 *     user_id = -1,
 *     now_path_id可以是任意参数，推荐填入-1
 *     op and args 都填NULL
 *     level: 成功_LOG_INFO; 失败_LOG_ERR
 *     info: NULL
 * 2）记录服务端调用的函数日志
 *      user_id = 0,
 *      now_path_id可以是任意参数，推荐填0
 *      op 必须写 传入调用的函数的字符串常量
 *      args 填需要的参数，如果没有为NULL，例如函数是malloc，申请一个client_info，那么 op = "malloc", args = "client_info"或者args = NULL
 *      level: 成功_LOG_INFO; 失败_LOG_ERR
 *      info：可以填NULL，或者是成功或者失败的备注信息的字符串
 * 3）记录8大命令操作的日志
 *      user_id 必须传入给定的user_id
 *      now_path_id 也要是传入的给定的now_path_id
 *      op 写命令的字符串常量 "cd" | "ls" 等等
 *      args 命令有参数，传入给定的args， 没有就NULL
 *      level: 成功_LOG_INFO; 失败_LOG_ERR
 *      info: 可以填NULL，或者是成功或者失败的备注信息的字符串
 */

int mysql_log(MYSQL *mysql, int user_id, int now_path_id, 
    const char *op, const char * args, const char *info, LOG_LEVEL level) {
    char operation[256] = {0};
    char info_field[256] = {0};
    char level_field[24] = {0};
    char sql[1024] = {0};
    ssize_t sret = 0;

    if(user_id == -1) {
        now_path_id = -1;
    } else if(user_id == 0) {
        now_path_id = 0;
    } 
    if(user_id == -1) {
        snprintf(operation, sizeof(operation), "server start");
    } else {
        sret = snprintf(operation, sizeof(operation), "%s", op);
        ERROR_CHECK(sret, -1, "snprintf");
        if(args != NULL) {
            sret = snprintf(operation + sret, sizeof(operation) - sret, " %s", args);
            ERROR_CHECK(sret, -1, "snprintf");
        }
    }
    if(info == NULL) {
        if(level == _LOG_INFO) {
            sret = snprintf(info_field, sizeof(info_field), "success");
            ERROR_CHECK(sret, -1, "snprintf");
        } else if(level == _LOG_ERR) {
            sret = snprintf(info_field, sizeof(info_field), "failure");
            ERROR_CHECK(sret, -1, "snprintf");
        }
    } else {
        sret = snprintf(info_field, sizeof(info_field), "%s", info);
        ERROR_CHECK(sret, -1, "snprintf");
    }

    if(level == _LOG_INFO) {
        sret = snprintf(level_field, sizeof(level_field), "LOG_INFO");
        ERROR_CHECK(sret, -1, "snprintf");
    } else if(level == _LOG_ERR) {
        sret = snprintf(level_field, sizeof(level_field), "LOG_ERR");
        ERROR_CHECK(sret, -1, "snprintf");
    }

    sret = snprintf(sql, sizeof(sql), "insert into log_data (user_id, now_path_id, operation, info, time, log_level) "\
        "values (%d, %d, '%s', '%s', NOW(), '%s');", user_id, now_path_id, operation, info_field, level_field);
    ERROR_CHECK(sret, -1, "snprintf");

    int qret = mysql_query(mysql, sql);
    MYSQL_ERROR_CHECK(qret, 0, mysql, "mysql_query: insert");

    return 0;
}

// int main() {
//     MYSQL *mysql = mysql_init(NULL);
//     MYSQL *pret = mysql_real_connect(mysql, "localhost", "admin", "123456", "user_base", 0, NULL, 0);
//     if(pret == NULL) {
//         fprintf(stderr, "%s:%s", "mysql_real_connect", mysql_error(mysql));
//         return 0;
//     }

//     mysql_log(mysql, -1, -1, NULL, NULL, NULL, _LOG_INFO);
//     mysql_log(mysql, 0, 0, "malloc", "client_info", NULL, _LOG_INFO);
//     mysql_log(mysql, 2, 4, "cd", "222", "没有目标路径", _LOG_ERR);

//     return 0;
// }